<?php
    include "init.php";
    if ($listid)
    {
        $listid		= $_POST[listid];
        $message	= $_POST[message];
        $message        = utf8_decode($message);

        mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database");
        @mysql_select_db("$DBName") or die("Unable to select database $DBName");
        $sqlquery = "select * from $TReceivers where ListId = '$listid' and CellPhone <> ''";
        $results = mysql_query($sqlquery) or die("Unable to query : " . mysql_error());

        while ($line = mysql_fetch_array($results, MYSQL_ASSOC))
        {
            $shortname	= $line["ShortName"];
            //$shortname	= utf8_decode($shortname);
            $cellphone	= $line["CellPhone"];
            $smsmessage = "Hey " . $shortname . " " . $message;

            // The neccesary variables are set.
            $url = "http://www.cpsms.dk/sms/";
            $url .= "?message=" . urlencode("$smsmessage");
            $url .= "&recipient=$cellphone"; // Recipient
            $url .= "&username=kvist"; // Username
            $url .= "&password=vi4veddet"; // Password
            $url .= "&from=" . urlencode("$fromphone"); // Sendername

            // The url is opened
            $reply  = file_get_contents($url);
            $reply .= " - $shortname";

            mysql_query("INSERT INTO $TSendSMS (ListId, CellPhone, Message, status)
                    VALUES ('$listid','$cellphone','$smsmessage','$reply')");

        }
        mysql_free_result($results);
        mysql_close();

        $to = "/smsstatus/$listid/$fromphone";
        header('Location: '. $to);
    }
?>